
//  Created by 莫瑞权 on 2022/5/13.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFWhiteboardTools: TalkfunHitTestView {
    @objc weak public var delegate: TFWhiteboardToolsDelegate?
//    @objc weak var delegate : TFWhiteboardToolsDelegate?
    //涂鸦类型选项
    let widthSelection = TFColorSelection()
    //颜色选择
    let colorSelection = TFColorSelection()
    //宽度选择
    let scrawlSelection = TFColorSelection()
    //默认的颜色
    let defaultColor = UIView()
    //默认涂鸦样式
    var scrawl = UIButton()
    var eraseButton = UIButton()
    //默认宽度view
    let widthView = UIView()
    //当前画板类型
    var chooseGraffitiTag:NSInteger = 0
    //一级按键数组
    let buttonArray = NSMutableArray()
    var drawingSize:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)

    
    
    func aaaaaa(_ btn:UIButton)
    {
        for i in 0..<self.buttonArray.count {
  
            
            let button = self.buttonArray[i] as! UIButton
            button.backgroundColor = UIColor(red: 13/255.0, green: 15/255.0, blue: 18/255.0, alpha: 1)
        }
        //隐藏颜色
        if (self.colorSelection.isHidden == false) {
            self.colorSelection.isHidden  = true
        }
        //隐藏宽度
        if (self.widthSelection.isHidden == false) {
            self.widthSelection.isHidden  = true
        }
        
         btn.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 1)
    }
    @objc func ClickEvents(_ btn:UIButton)
    {
        
        //画线
        if (btn.tag==0) {
            //默认色
            
            self.eraseButton.backgroundColor = UIColor(red: 13/255.0, green: 15/255.0, blue: 18/255.0, alpha: 1)
          
                self.scrawlSelection.isHidden  = false
                self.widthSelection.isHidden  = true
                self.colorSelection.isHidden  = true
           
            if self.delegate != nil && (self.delegate?.responds(to: #selector(TFWhiteboardToolsDelegate.chooseGraffiti(_:))))!{
              
                self.delegate?.chooseGraffiti(self.chooseGraffitiTag)
            }
        
        }
        
        //擦除
        else  if (btn.tag==1) {
            self.aaaaaa(btn)
            
            self.scrawlSelection.isHidden  = true
        
            if self.delegate != nil && (self.delegate?.responds(to: #selector(TFWhiteboardToolsDelegate.chooseEraser)))!{
              
                self.delegate?.chooseEraser()
            }
           
        }
        else if (btn.tag==2) {
            //涂鸦类型选项
    
                self.widthSelection.isHidden  = false
          
            //颜色选择
          
                self.colorSelection.isHidden  = true
          
            //宽度选择
       
                self.scrawlSelection.isHidden  = true
      
           
        }else if (btn.tag==3) {
            //涂鸦颜色
                self.colorSelection.isHidden  = false
        
                self.widthSelection.isHidden  = true
     
                self.scrawlSelection.isHidden  = true
            
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        for i in stride(from: 0, to: 4 ,by: 1) {
      
            let button = UIButton()
            
             button.backgroundColor = UIColor(red: 13/255.0, green: 15/255.0, blue: 18/255.0, alpha: 1)
            self.addSubview(button)
            self.buttonArray.add(button)
            button.addTarget(self, action: #selector(self.ClickEvents), for: .touchUpInside)
          
         }
        self.addSubview(self.widthSelection)

        self.addSubview(self.widthView)
         
         //颜色选择
  
        self.addSubview(self.scrawlSelection)

        self.addSubview(self.defaultColor)
         //颜色选择

         self.defaultColor.backgroundColor = UIColor(red:248.0/255.0, green: 70.0/255.0, blue: 70.0/255.0, alpha: 1)
       
        self.addSubview(self.colorSelection)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.maskPath(self)
    
        self.backgroundColor = UIColor.clear
       
        let buttonW:CGFloat = 34.0
        let spacing:CGFloat = 8


        let Y:CGFloat = (self.frame.size.height-( buttonW*4)-(spacing*3))/2
            
        for i in 0..<self.buttonArray.count {
  
            let button = self.buttonArray[i] as! UIButton
                    button.tag = i
                    
            let  width:CGFloat = buttonW;
            let  x:CGFloat = self.frame.size.width-width
            let  y:CGFloat = (CGFloat(i)*width)+(spacing*CGFloat(i))+Y
            button.frame   = CGRect(x: x, y: y, width: width, height: width)
            self.maskPath(button)
                
                  
            if (i==0) {
                    if  self.scrawl != button {
                        self.scrawl = button;
                    }
                        
                        self.scrawl.setImage(UIImage.init(named: "HTLive.bundle/S_1vN"), for: .normal)
                        
                        self.scrawlSelection.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
                        
                        self.scrawlSelection.isHidden = true
                
                    let colorSelectionY:CGFloat = y ;
                    let colorSelectionW:CGFloat = width*5;
                    let colorSelectionX:CGFloat = (self.frame.origin.x+self.frame.size.width)-colorSelectionW;
                    let  colorSelectionH:CGFloat = width
                        self.scrawlSelection.frame = CGRect(x: colorSelectionX, y: colorSelectionY, width: colorSelectionW, height: colorSelectionH)
                    self.scrawlSelection.scrawlSelectionAddTarget(self, action: #selector(self.scrawlSelectionClick))
                        
                
            }
            else if (i==1) {
             
                    if  self.eraseButton != button {
                        self.eraseButton = button
                    }
                    
                          
                    self.eraseButton.backgroundColor = UIColor(red: 13/255.0, green: 15/255.0, blue: 18/255.0 , alpha: 1)
                
                    self.eraseButton.setImage(UIImage.init(named: "HTLive.bundle/erase"), for: .normal)
                       
                   
                    }
                    else if (i==2) {
                       
                        self.widthView.isUserInteractionEnabled = false
                        self.widthView.backgroundColor = UIColor.white
                       
                        
                        var vcW:CGFloat = spacing
                        if (self.drawingSize.size.width>0) {
                            vcW = self.drawingSize.size.width
                        }
                        let vcH:CGFloat =  vcW
                        
                        let vcX:CGFloat = x+((width-vcW)/2)
                        let vcY:CGFloat = y+((width-vcW)/2)
                        
                        self.widthView.frame = CGRect(x: vcX, y: vcY, width: vcW, height: vcH)
                    
                        //超出主层边框的都会被裁减调用
                        self.widthView.layer.cornerRadius = vcW/2
                       // 设置裁剪
                        self.widthView.layer.masksToBounds = true
                    
                        self.widthSelection.isHidden = true
                        self.widthSelection.backgroundColor = button.backgroundColor
                       
                        let widthSelectionY:CGFloat =  y
                        let widthSelectionW:CGFloat = width*4
                        let widthSelectionX:CGFloat = (self.frame.origin.x+self.frame.size.width)-widthSelectionW
                        let widthSelectionH:CGFloat = width
                        
                        self.widthSelection.frame = CGRect(x: widthSelectionX, y: widthSelectionY, width: widthSelectionW, height: widthSelectionH)
                     
                        
                        //宽度添加点击事件
                        self.widthSelection.WidthSelectionAddTarget(self, action: #selector(self.controlSelect))
                      
                        
                        self.widthView.layer.cornerRadius = self.widthView.bounds.size.width/2.0
                        self.widthView.layer.masksToBounds = true
                    }
                    else if (i==3) {
           
                        let vcW:CGFloat = button.frame.size.width/2
                        let vcH:CGFloat = vcW
                        let vcX:CGFloat = x+((width-vcW)/2)
                        let vcY:CGFloat = y+((width-vcW)/2)
                         self.defaultColor.frame = CGRect(x: vcX, y: vcY, width: vcW, height: vcH)
                  
                         self.defaultColor.isUserInteractionEnabled = false
             
                       
                        self.colorSelection.backgroundColor = button.backgroundColor
                        
                        self.colorSelection.isHidden = true
                  
                        let colorSelectionY:CGFloat = y
                        let colorSelectionW:CGFloat = width*5;
                        let colorSelectionX:CGFloat = (self.frame.origin.x+self.frame.size.width)-colorSelectionW
                        let  colorSelectionH:CGFloat = width
                        self.colorSelection.frame = CGRect(x: colorSelectionX, y: colorSelectionY, width: colorSelectionW, height: colorSelectionH)
                   
                        self.colorSelection.colorSelectionAddTarget(self, action: #selector(self.chooseColor))
                    }
                     
                }
            
    }
    
    func maskPath(_ vc:UIView)
    {
        let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners:[UIRectCorner.bottomLeft,UIRectCorner.topLeft], cornerRadii: CGSize(width: 4, height: 4))
       
        let maskLayer = CAShapeLayer()
        maskLayer.frame = vc.bounds
        maskLayer.path = maskPath.cgPath
        vc.layer.mask = maskLayer
    }
    
   // #pragma mark类型选择
   @objc func scrawlSelectionClick(_ btn:TFSelectionButton)

    {
        self.scrawl.setImage(btn.selectionImage, for: .normal)
       
        self.chooseGraffitiTag = btn.tag
        self.scrawlSelection.isHidden  = true

        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFWhiteboardToolsDelegate.chooseGraffiti(_:))))!{
            //点击了涂鸦
            self.delegate?.chooseGraffiti(btn.tag)
        }

        
    }
    
    // #pragma mark颜色选择
    @objc func chooseColor(_ btn:TFSelectionButton)
     {

          self.defaultColor.backgroundColor = btn.selectionColor
          self.colorSelection.isHidden  = true
         
             
             if self.delegate != nil && (self.delegate?.responds(to: #selector(TFWhiteboardToolsDelegate.chooseColor(_:))))!{
                 
             
                 //点击了涂鸦
                 self.delegate?.chooseColor(btn.selectionColor)
             }
             
         
     }
    
  //  #pragma mark 宽度选择
    @objc  func controlSelect(_ btn:TFSelectionButton)
    {
        var buttonW:CGFloat = 34.0
        if (UIDevice.current.userInterfaceIdiom == .pad)
          {
            buttonW = 50
         }
       
      let width1:CGFloat = buttonW/4
        
        
        if (btn.tag == 0) {
            
            self.drawingSize = CGRect(x: 0, y: 0, width: width1*( 1/4.0), height: width1*(1/4.0))
           
            
        }else  if (btn.tag == 1) {
            
            self.drawingSize = CGRect(x: 0, y: 0, width: width1*( 2/4.0), height: width1*(2/4.0))
        }
        else  if (btn.tag == 2) {
            
            self.drawingSize = CGRect(x: 0, y: 0, width: width1*( 3/4.0), height: width1*(3/4.0))
        }
        else  if (btn.tag == 3) {
            
            self.drawingSize = CGRect(x: 0, y: 0, width: width1, height: width1)
        }
        
        self.widthView.layer.cornerRadius = self.widthView.bounds.size.width/2.0
        self.widthView.layer.masksToBounds = true
     
        self.widthSelection.isHidden  = true
        
        self.widthSelection.bounds = CGRect(x: 0, y: 0, width: self.drawingSize.size.width, height: self.drawingSize.size.height)
        
        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFWhiteboardToolsDelegate.chooseWidth(_:))))!{
            //点击了宽度
            self.delegate?.chooseWidth(btn.tag)
        }

}

}

@objc public protocol TFWhiteboardToolsDelegate : NSObjectProtocol {
    //点击了涂鸦类型
    func chooseGraffiti(_ tag : NSInteger)
    //选择了橡皮擦
    func chooseEraser()
    //宽度选择
    func chooseWidth(_ tag : NSInteger)
    //颜色选择
    func chooseColor(_ rgbColor : UIColor)


}
